<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Chapter 9 Additional advanced plots and customisation options | Data visualisation using R, for researchers who don’t use R</title>
<meta name="author" content="Emily Nordmann, Phil McAleer, Wilhelmiina Toivo, Helena Paterson, Lisa DeBruine">
<meta name="description" content="9.1 Adding lines to plots Vertical Lines - geom_vline() Often it can be useful to put a marker into our plots to highlight a certain criterion value. For example, if you were working with a scale...">
<meta name="generator" content="bookdown 0.23 with bs4_book()">
<meta property="og:title" content="Chapter 9 Additional advanced plots and customisation options | Data visualisation using R, for researchers who don’t use R">
<meta property="og:type" content="book">
<meta property="og:url" content="https://psyteachr.github.io/introdataviz/additional-advanced-plots-and-customisation-options.html">
<meta property="og:image" content="https://psyteachr.github.io/introdataviz/images/logos/logo.png">
<meta property="og:description" content="9.1 Adding lines to plots Vertical Lines - geom_vline() Often it can be useful to put a marker into our plots to highlight a certain criterion value. For example, if you were working with a scale...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Chapter 9 Additional advanced plots and customisation options | Data visualisation using R, for researchers who don’t use R">
<meta name="twitter:description" content="9.1 Adding lines to plots Vertical Lines - geom_vline() Often it can be useful to put a marker into our plots to highlight a certain criterion value. For example, if you were working with a scale...">
<meta name="twitter:image" content="https://psyteachr.github.io/introdataviz/images/logos/logo.png">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/header-attrs-2.10.6/header-attrs.js"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.2.5.1/tabs.js"></script><script src="libs/bs3compat-0.2.5.1/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=G-6NP3MF25W3"></script><script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-6NP3MF25W3');
    </script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- CSS --><link rel="stylesheet" href="include/psyteachr.css">
<link rel="stylesheet" href="include/webex.css">
<link rel="stylesheet" href="include/style.css">
</head>
<body data-spy="scroll" data-target="#toc">

<div class="container-fluid">
<div class="row">
  <header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>

    <div class="d-flex align-items-start justify-content-between">
      <h1>
        <a href="index.html" title="">Data visualisation using R, for researchers who don’t use R</a>
      </h1>
      <button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
    </div>

    <div id="main-nav" class="collapse-lg">
      <form role="search">
        <input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>

      <nav aria-label="Table of contents"><h2>Table of contents</h2>
        <ul class="book-toc list-unstyled">
<li><a class="" href="index.html">Overview</a></li>
<li><a class="" href="introduction.html"><span class="header-section-number">1</span> Introduction</a></li>
<li><a class="" href="getting-started.html"><span class="header-section-number">2</span> Getting Started</a></li>
<li><a class="" href="transforming-data.html"><span class="header-section-number">3</span> Transforming Data</a></li>
<li><a class="" href="representing-summary-statistics.html"><span class="header-section-number">4</span> Representing Summary Statistics</a></li>
<li><a class="" href="multi-part-plots.html"><span class="header-section-number">5</span> Multi-part Plots</a></li>
<li><a class="" href="advanced-plots.html"><span class="header-section-number">6</span> Advanced Plots</a></li>
<li><a class="" href="conclusion.html"><span class="header-section-number">7</span> Conclusion</a></li>
<li><a class="" href="appendices.html">Appendices</a></li>
<li><a class="" href="additional-resources.html"><span class="header-section-number">8</span> Additional resources</a></li>
<li><a class="active" href="additional-advanced-plots-and-customisation-options.html"><span class="header-section-number">9</span> Additional advanced plots and customisation options</a></li>
</ul>

        <div class="book-extra">
          <p><a id="book-repo" href="https://github.com/psyteachr/introdataviz">View book source <i class="fab fa-github"></i></a></p>
        </div>
      </nav>
</div>
  </header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="additional-advanced-plots-and-customisation-options" class="section level1" number="9">
<h1>
<span class="header-section-number">9</span> Additional advanced plots and customisation options<a class="anchor" aria-label="anchor" href="#additional-advanced-plots-and-customisation-options"><i class="fas fa-link"></i></a>
</h1>
<div id="adding-lines-to-plots" class="section level2" number="9.1">
<h2>
<span class="header-section-number">9.1</span> Adding lines to plots<a class="anchor" aria-label="anchor" href="#adding-lines-to-plots"><i class="fas fa-link"></i></a>
</h2>
<p><strong>Vertical Lines - geom_vline()</strong></p>
<p>Often it can be useful to put a marker into our plots to highlight a certain criterion value. For example, if you were working with a scale that has a cut-off, perhaps the Austim Spectrum Quotient 10 <span class="citation">(<a href="additional-advanced-plots-and-customisation-options.html#ref-allison2012toward" role="doc-biblioref">Allison et al., 2012</a>)</span>, then you might want to put a line at a score of 7; the point at which the researchers suggest the participant is referred further. Alternatively, thinking about the Stroop test we have looked at in this paper, perhaps you had a level of accuracy that you wanted to make sure was reached - let's say 80%. If we refer back to Figure <a href="transforming-data.html#fig:histograms">3.1</a>, which used the code below:</p>
<div class="sourceCode" id="cb81"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">acc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">1</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Accuracy (0-100)"</span><span class="op">)</span></code></pre></div>
<p>and displayed the spread of the accuracy scores as such:</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:histogram-acc-vline1"></span>
<img src="appendix-0_files/figure-html/histogram-acc-vline1-1.png" alt="Histogram of accuracy scores." width="100%"><p class="caption">
Figure 9.1: Histogram of accuracy scores.
</p>
</div>
<p>if we wanted to add a line at the 80% level then we could use the <code>geom_vline()</code> function, again from the <strong><code>ggplot2</code></strong>, with the argument of <code>xintercept = 80</code>, meaning cut the x-axis at 80, as follows:</p>
<div class="sourceCode" id="cb82"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">acc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">1</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Accuracy (0-100)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_vline</span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fl">80</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:histogram-acc-vline2"></span>
<img src="appendix-0_files/figure-html/histogram-acc-vline2-1.png" alt="Histogram of accuracy scores with black solid vertical line indicating 80% accuracy." width="100%"><p class="caption">
Figure 9.2: Histogram of accuracy scores with black solid vertical line indicating 80% accuracy.
</p>
</div>
<p>Now that looks ok but the line is a bit hard to see so we can change the style (<code>linetype = value</code>), color (<code>color = "color"</code>) and weight (<code>size = value</code>) as follows:</p>
<div class="sourceCode" id="cb83"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">acc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">1</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Accuracy (0-100)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_vline</span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fl">80</span>, linetype <span class="op">=</span> <span class="fl">2</span>, color <span class="op">=</span> <span class="st">"red"</span>, size <span class="op">=</span> <span class="fl">1.5</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:histogram-acc-vline3"></span>
<img src="appendix-0_files/figure-html/histogram-acc-vline3-1.png" alt="Histogram of accuracy scores with red dashed vertical line indicating 80% accuracy." width="100%"><p class="caption">
Figure 9.3: Histogram of accuracy scores with red dashed vertical line indicating 80% accuracy.
</p>
</div>
<p><strong>Horizontal Lines - geom_hline()</strong></p>
<p>Another situation may be that you want to put a horizontal line on your figure to mark a value of interest on the y-axis. Again thinking about our Stroop experiment, perhaps we wanted to indicate the 80% accuracy line on our boxplot figures. If we look at Figure <a href="representing-summary-statistics.html#fig:boxplot1">4.1</a>, which used this code to display the basic boxplot:</p>
<div class="sourceCode" id="cb84"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y <span class="op">=</span> <span class="va">acc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:boxplot1-add"></span>
<img src="appendix-0_files/figure-html/boxplot1-add-1.png" alt="Basic boxplot." width="100%"><p class="caption">
Figure 9.4: Basic boxplot.
</p>
</div>
<p>we could then use the <code>geom_hline()</code> function, from the <strong><code>ggplot2</code></strong>, with, this time, the argument of <code>yintercept = 80</code>, meaning cut the y-axis at 80, as follows:</p>
<div class="sourceCode" id="cb85"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y <span class="op">=</span> <span class="va">acc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_hline</span><span class="op">(</span>yintercept <span class="op">=</span> <span class="fl">80</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:boxplot1-hline1"></span>
<img src="appendix-0_files/figure-html/boxplot1-hline1-1.png" alt="Basic boxplot with black solid horizontal line indicating 80% accuracy." width="100%"><p class="caption">
Figure 9.5: Basic boxplot with black solid horizontal line indicating 80% accuracy.
</p>
</div>
<p>and again we can embellish the line using the same arguments as above. We will put in some different values here just to show the changes:</p>
<div class="sourceCode" id="cb86"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y <span class="op">=</span> <span class="va">acc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_hline</span><span class="op">(</span>yintercept <span class="op">=</span> <span class="fl">80</span>, linetype <span class="op">=</span> <span class="fl">3</span>, color <span class="op">=</span> <span class="st">"blue"</span>, size <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:boxplot1-hline2"></span>
<img src="appendix-0_files/figure-html/boxplot1-hline2-1.png" alt="Basic boxplot with blue dotted horizontal line indicating 80% accuracy." width="100%"><p class="caption">
Figure 9.6: Basic boxplot with blue dotted horizontal line indicating 80% accuracy.
</p>
</div>
<p><strong>LineTypes</strong></p>
<p>One thing worth noting is that the <code>linetype</code> argument can actually be specified as both a value or as a word. They match up as follows:</p>
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th align="center">Value</th>
<th align="center">Word</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="center">linetype = 0</td>
<td align="center">linetype = "blank"</td>
</tr>
<tr class="even">
<td align="center">linetype = 1</td>
<td align="center">linetype = "solid"</td>
</tr>
<tr class="odd">
<td align="center">linetype = 2</td>
<td align="center">linetype = "dashed"</td>
</tr>
<tr class="even">
<td align="center">linetype = 3</td>
<td align="center">linetype = "dotted"</td>
</tr>
<tr class="odd">
<td align="center">linetype = 4</td>
<td align="center">linetype = "dotdash"</td>
</tr>
<tr class="even">
<td align="center">linetype = 5</td>
<td align="center">linetype = "longdash"</td>
</tr>
<tr class="odd">
<td align="center">linetype = 6</td>
<td align="center">linetype = "twodash"</td>
</tr>
</tbody>
</table></div>
<p><strong>Diagonal Lines - geom_abline()</strong></p>
<p>The last type of line you might want to overlay on a figure is perhaps a diagonal line. For example, perhaps you have created a scatterplot and you want to have the true diagonal line for reference to the line of best fit. To show this, we will refer back to Figure <a href="transforming-data.html#fig:smooth-plot">3.5</a> which displayed the line of best fit for the reaction time versus age, and used the following code:</p>
<div class="sourceCode" id="cb87"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-add"></span>
<img src="appendix-0_files/figure-html/smooth-plot-add-1.png" alt="Line of best fit for reaction time versus age." width="100%"><p class="caption">
Figure 9.7: Line of best fit for reaction time versus age.
</p>
</div>
<p>By eye that would appear to be a fairly flat relationship but we will add the true diagonal to help clarify. To do this we use the <code>geom_abline()</code>, again from <strong><code>ggplot2</code></strong>, and we give it the arguements of the slope (<code>slope  = value</code>) and the intercept (<code>intercept = value</code>). We are also going to scale the data to turn it into z-scores to help us visualise the relationship better, as follows:</p>
<div class="sourceCode" id="cb88"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">dat_long_scale</span> <span class="op">&lt;-</span> <span class="va">dat_long</span> <span class="op">%&gt;%</span>
  <span class="fu">mutate</span><span class="op">(</span>rt_zscore <span class="op">=</span> <span class="op">(</span><span class="va">rt</span> <span class="op">-</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">rt</span><span class="op">)</span><span class="op">)</span><span class="op">/</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op">(</span><span class="va">rt</span><span class="op">)</span>,
         age_zscore <span class="op">=</span> <span class="op">(</span><span class="va">age</span> <span class="op">-</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">age</span><span class="op">)</span><span class="op">)</span><span class="op">/</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op">(</span><span class="va">age</span><span class="op">)</span><span class="op">)</span>

<span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long_scale</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt_zscore</span>, y <span class="op">=</span> <span class="va">age_zscore</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_abline</span><span class="op">(</span>slope <span class="op">=</span> <span class="fl">1</span>, intercept <span class="op">=</span> <span class="fl">0</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-abline1"></span>
<img src="appendix-0_files/figure-html/smooth-plot-abline1-1.png" alt="Line of best fit (blue line) for reaction time versus age with true diagonal shown (black line)." width="100%"><p class="caption">
Figure 9.8: Line of best fit (blue line) for reaction time versus age with true diagonal shown (black line).
</p>
</div>
<p>So now we can see the line of best fit (blue line) in relation to the true diagonal (black line). We will come back to why we z-scored the data in a minute, but first let's finish tidying up this figure, using some of the customisation we have seen as it is a bit messy. Something like this might look cleaner:</p>
<div class="sourceCode" id="cb89"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long_scale</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt_zscore</span>, y <span class="op">=</span> <span class="va">age_zscore</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_abline</span><span class="op">(</span>slope <span class="op">=</span> <span class="fl">1</span>, intercept <span class="op">=</span> <span class="fl">0</span>, linetype <span class="op">=</span> <span class="st">"dashed"</span>, color <span class="op">=</span> <span class="st">"black"</span>, size <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_hline</span><span class="op">(</span>yintercept <span class="op">=</span> <span class="fl">0</span>, linetype <span class="op">=</span> <span class="st">"solid"</span>, color <span class="op">=</span> <span class="st">"black"</span>, size <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_vline</span><span class="op">(</span>xintercept <span class="op">=</span> <span class="fl">0</span>, linetype <span class="op">=</span> <span class="st">"solid"</span>, color <span class="op">=</span> <span class="st">"black"</span>, size <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
    <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-abline2"></span>
<img src="appendix-0_files/figure-html/smooth-plot-abline2-1.png" alt="Line of best fit (blue solid line) for reaction time versus age with true diagonal shown (black line dashed)." width="100%"><p class="caption">
Figure 9.9: Line of best fit (blue solid line) for reaction time versus age with true diagonal shown (black line dashed).
</p>
</div>
<p>That maybe looks a bit cluttered but it gives a nice example of how you can use the different geoms for adding lines to add information to your figure, clearly visualising the weak relationship between reaction time and age. <strong>Note:</strong> Do remember about the layering system however; you will notice that in the code for Figure <a href="additional-advanced-plots-and-customisation-options.html#fig:smooth-plot-abline2">9.9</a> we have changed the order of the code lines so that the geom lines are behind the points!</p>
<p><strong>Top Tip: Your intercepts must be values you can see</strong></p>
<p>Thinking back to why we z-scored the data for that last figure, we sort of skipped over that, but it did serve a purpose. Here is the original data and the original scatterplot but with the <code>geom_abline()</code> added to the code:</p>
<div class="sourceCode" id="cb90"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_abline</span><span class="op">(</span>slope <span class="op">=</span> <span class="fl">1</span>, intercept <span class="op">=</span> <span class="fl">0</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-abline3"></span>
<img src="appendix-0_files/figure-html/smooth-plot-abline3-1.png" alt="Line of best fit (blue solid line) for reaction time versus age with missing true diagonal." width="100%"><p class="caption">
Figure 9.10: Line of best fit (blue solid line) for reaction time versus age with missing true diagonal.
</p>
</div>
<p>The code runs but the diagonal line is nowhere to be seen. The reason is that you figure is zoomed in on the data and the diagonal is "out of shot" if you like. If we were to zoom out on the data we would then see the diagonal line as such:</p>
<div class="sourceCode" id="cb91"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_abline</span><span class="op">(</span>slope <span class="op">=</span> <span class="fl">1</span>, intercept <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">coord_cartesian</span><span class="op">(</span>xlim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span><span class="op">)</span>, ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">60</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-abline4"></span>
<img src="appendix-0_files/figure-html/smooth-plot-abline4-1.png" alt="Zoomed out to show Line of best fit (blue solid line) for reaction time versus age with true diagonal (black line)." width="100%"><p class="caption">
Figure 9.11: Zoomed out to show Line of best fit (blue solid line) for reaction time versus age with true diagonal (black line).
</p>
</div>
<p>So the key point is that your intercepts have to be set to visible for values for you to see them! If you run your code and the line does not appear, check that the value you have set can actually be seen on your figure. This applies to <code>geom_abline()</code>, <code>geom_hline()</code> and <code>geom_vline()</code>.</p>
</div>
<div id="zooming-in-and-out" class="section level2" number="9.2">
<h2>
<span class="header-section-number">9.2</span> Zooming in and out<a class="anchor" aria-label="anchor" href="#zooming-in-and-out"><i class="fas fa-link"></i></a>
</h2>
<p>Like in the example above, it can be very beneficial to be able to zoom in and out of figures, mainly to focus the frame on a given section. One function we can use to do this is the <code>coord_cartesian()</code>, in <strong><code>ggplot2</code></strong>. The main arguments are the limits on the x-axis (<code>xlim = c(value, value)</code>), the limits on the y-axis (<code>ylim = c(value, value)</code>), and whether to add a small expansion to those limits or not (<code>expand = TRUE/FALSE</code>). Looking at the scatterplot of age and reaction time again, we could use <code>coord_cartesian()</code> to zoom fully out:</p>
<div class="sourceCode" id="cb92"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">coord_cartesian</span><span class="op">(</span>xlim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span><span class="op">)</span>, ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">100</span><span class="op">)</span>, expand <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-coord1"></span>
<img src="appendix-0_files/figure-html/smooth-plot-coord1-1.png" alt="Zoomed out on scatterplot with no expansion around set limits" width="100%"><p class="caption">
Figure 9.12: Zoomed out on scatterplot with no expansion around set limits
</p>
</div>
<p>And we can add a small expansion by changing the <code>expand</code> argument to <code>TRUE</code>:</p>
<div class="sourceCode" id="cb93"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">coord_cartesian</span><span class="op">(</span>xlim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span><span class="op">)</span>, ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">100</span><span class="op">)</span>, expand <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-coord2"></span>
<img src="appendix-0_files/figure-html/smooth-plot-coord2-1.png" alt="Zoomed out on scatterplot with small expansion around set limits" width="100%"><p class="caption">
Figure 9.13: Zoomed out on scatterplot with small expansion around set limits
</p>
</div>
<p>Or we can zoom right in on a specific area of the plot if there was something we wanted to highlight. Here for example we are just showing the reaction times between 500 and 725 msecs, and all ages between 15 and 55:</p>
<div class="sourceCode" id="cb94"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">coord_cartesian</span><span class="op">(</span>xlim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">500</span>,<span class="fl">725</span><span class="op">)</span>, ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">15</span>,<span class="fl">55</span><span class="op">)</span>, expand <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-coord3"></span>
<img src="appendix-0_files/figure-html/smooth-plot-coord3-1.png" alt="Zoomed in on scatterplot with small expansion around set limits" width="100%"><p class="caption">
Figure 9.14: Zoomed in on scatterplot with small expansion around set limits
</p>
</div>
<p>And you can zoom in and zoom out just the x-axis or just the y-axis; just depends on what you want to show.</p>
</div>
<div id="setting-the-axis-values" class="section level2" number="9.3">
<h2>
<span class="header-section-number">9.3</span> Setting the axis values<a class="anchor" aria-label="anchor" href="#setting-the-axis-values"><i class="fas fa-link"></i></a>
</h2>
<p><strong>Continuous scales</strong></p>
<p>You may have noticed that depending on the spread of your data, and how much of the figure you see, the values on the axes tend to change. Often we don't want this and want the values to be constant. We have already used functions to control this in the main body of the paper - the <code>scale_*</code> functions. Here we will use <code>scale_x_continuous()</code> and <code>scale_y_continuous()</code> to set the values on the axes to what we want. The main arguments in both functions are the limits (<code>limts = c(value, value)</code>) and the breaks (the tick marks essentially, <code>breaks = value:value</code>). Note that the limits are just two values (minimum and maximum), whereas the breaks are a series of values (from 0 to 100, for example). If we use the scatterplot of age and reaction time, then our code might look like this:</p>
<div class="sourceCode" id="cb95"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span><span class="op">)</span>, breaks <span class="op">=</span> <span class="fl">0</span><span class="op">:</span><span class="fl">1000</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_y_continuous</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">100</span><span class="op">)</span>, breaks <span class="op">=</span> <span class="fl">0</span><span class="op">:</span><span class="fl">100</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-scales1"></span>
<img src="appendix-0_files/figure-html/smooth-plot-scales1-1.png" alt="Changing the values on the axes" width="100%"><p class="caption">
Figure 9.15: Changing the values on the axes
</p>
</div>
<p>That actually looks rubbish because we simply have too many values on our axes, so we can use the <code><a href="https://rdrr.io/r/base/seq.html">seq()</a></code> function, from <strong><code>baseR</code></strong>, to get a bit more control. The arguments here are the first value (<code>from = value</code>), the last value (<code>last = value</code>), and the size of the steps (<code>by = value</code>). For example, <code><a href="https://rdrr.io/r/base/seq.html">seq(0,10,2)</a></code> would give all values between 0 and 10 in steps of 2, (i.e. 0, 2, 4, 6, 8 and 10). So using that idea we can change the y-axis in steps of 5 (years) and the x-axis in steps of 50 (msecs) as follows:</p>
<div class="sourceCode" id="cb96"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span><span class="op">)</span>, breaks <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span>,<span class="fl">50</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_y_continuous</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">100</span><span class="op">)</span>, breaks <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">100</span>,<span class="fl">5</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-scales2"></span>
<img src="appendix-0_files/figure-html/smooth-plot-scales2-1.png" alt="Changing the values on the axes using the seq() function" width="100%"><p class="caption">
Figure 9.16: Changing the values on the axes using the seq() function
</p>
</div>
<p>Which gives us a much nicer and cleaner set of values on our axes. And if we combine that approach for setting the axes values with our zoom function (<code>coord_cartesian()</code>), then we can get something that looks like this:</p>
<div class="sourceCode" id="cb97"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span><span class="op">)</span>, breaks <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">1000</span>,<span class="fl">50</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_y_continuous</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">100</span><span class="op">)</span>, breaks <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">100</span>,<span class="fl">5</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">coord_cartesian</span><span class="op">(</span>xlim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">250</span>,<span class="fl">750</span><span class="op">)</span>, ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">15</span>,<span class="fl">55</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-scales3"></span>
<img src="appendix-0_files/figure-html/smooth-plot-scales3-1.png" alt="Combining scale functions and zoom functions" width="100%"><p class="caption">
Figure 9.17: Combining scale functions and zoom functions
</p>
</div>
<p>Which actually looks much like our original scatterplot but with better definition on the axes. So you can see we can actually have a lot of control over the axes and what we see. However, one thing to note, is that you should not use the <code>limits</code> argument within the <code>scale_*</code> functions as a zoom. It won't work like that and instead will just disregard data. Look at this example:</p>
<div class="sourceCode" id="cb98"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">500</span>,<span class="fl">600</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<pre><code>## Warning: Removed 166 rows containing non-finite values (stat_smooth).</code></pre>
<pre><code>## Warning: Removed 166 rows containing missing values (geom_point).</code></pre>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot-scales4"></span>
<img src="appendix-0_files/figure-html/smooth-plot-scales4-1.png" alt="Combining scale functions and zoom functions" width="100%"><p class="caption">
Figure 9.18: Combining scale functions and zoom functions
</p>
</div>
<p>It may look like it has zoomed in on the data but actually it has removed all data outwith the limits. That is what the warnings are telling you, and you can see that as there is no data above and below the limits, but we know there should be based on the earlier plots. So <code>scale_*</code> functions can change the values on the axes, but <code>coord_cartesian()</code> is for zooming in and out.</p>
<p><strong>Discrete scales</strong></p>
<p>The same idea of <code>limits</code> within a <code>scale_*</code> function can also be used to change the order of categories on a discrete scale. For example if we look at our boxplots again in Figure <a href="representing-summary-statistics.html#fig:viobox6">4.12</a>, we see this figure:</p>
<div class="sourceCode" id="cb101"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, alpha <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-add"></span>
<img src="appendix-0_files/figure-html/viobox6-add-1.png" alt="Using transparency on the fill color." width="100%"><p class="caption">
Figure 9.19: Using transparency on the fill color.
</p>
</div>
<p>The figures always default to the alphabetical order. Sometimes that is what we want; sometimes that is not what we want. If we wanted to switch the order of <strong>word</strong> and <strong>non-word</strong> so that the non-word condition comes first we would use the <code>scale_x_discrete()</code> function and set the limits within it (<code>limits = c("category","category")</code>) as follows:</p>
<div class="sourceCode" id="cb102"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, alpha <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_discrete</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"nonword"</span>,<span class="st">"word"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-scale1"></span>
<img src="appendix-0_files/figure-html/viobox6-scale1-1.png" alt="Switching orders of categorical variables" width="100%"><p class="caption">
Figure 9.20: Switching orders of categorical variables
</p>
</div>
<p>And that works just the same if you have more conditions, which you will see if you compare Figure <a href="representing-summary-statistics.html#fig:viobox4">4.10</a> to the below figure where we have flipped the order of non-word and word from the original default alphabetical order</p>
<div class="sourceCode" id="cb103"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">language</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, position <span class="op">=</span> <span class="fu">position_dodge</span><span class="op">(</span><span class="fl">.9</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span>, 
               position <span class="op">=</span> <span class="fu">position_dodge</span><span class="op">(</span><span class="fl">.9</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span>,
               position <span class="op">=</span> <span class="fu">position_dodge</span><span class="op">(</span><span class="fl">.9</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_discrete</span><span class="op">(</span>limits <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"nonword"</span>,<span class="st">"word"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox4-scale1"></span>
<img src="appendix-0_files/figure-html/viobox4-scale1-1.png" alt="Same as earlier figure but with order of conditions on x-axis altered." width="100%"><p class="caption">
Figure 9.21: Same as earlier figure but with order of conditions on x-axis altered.
</p>
</div>
<p><strong>Changing Order of Factors</strong></p>
<p>Again, you have a lot of control beyond the default alphabetical order that <strong><code>ggplot2</code></strong> tends to plot in. One question you might have though is why <strong>monolingual</strong> and <strong>bilingual</strong> are not in alphabetical order? f they were then the <strong>bilingual</strong> condition would be plotted first. The answer is, thinking back to the start of the paper, we changed our conditions from <strong>1</strong> and <strong>2</strong> to the factor names of <strong>monolingual</strong> and <strong>bilingual</strong>, and <strong><code>ggplot()</code></strong> maintains that factor order when plotting. So if we want to plot it in a different fashion we need to do a bit of factor reordering. This can be done much like earlier using the <code><a href="https://rdrr.io/r/base/factor.html">factor()</a></code> function and stating the order of conditions we want (<code>levels = c("factor","factor")</code>). But be careful with spelling as it must match up to the names of the factors that already exist.</p>
<p>In this example, we will reorder the factors so that <strong>bilingual</strong> is presented first but leave the order of <strong>word</strong> and <strong>non-word</strong> as the alphabetical default. Note in the code though that we are not permanently storing the factor change as we don't want to keep this new order. We are just changing the order "on the fly" for this one example before putting it into the plot.</p>
<div class="sourceCode" id="cb104"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">dat_long</span> <span class="op">%&gt;%</span> 
  <span class="fu">mutate</span><span class="op">(</span>language <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">language</span>, 
                           levels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"bilingual"</span>,<span class="st">"monolingual"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu">ggplot</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">language</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, position <span class="op">=</span> <span class="fu">position_dodge</span><span class="op">(</span><span class="fl">.9</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span>, 
               position <span class="op">=</span> <span class="fu">position_dodge</span><span class="op">(</span><span class="fl">.9</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span>,
               position <span class="op">=</span> <span class="fu">position_dodge</span><span class="op">(</span><span class="fl">.9</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox4-scale2"></span>
<img src="appendix-0_files/figure-html/viobox4-scale2-1.png" alt="Same as earlier figure but with order of conditions on x-axis altered." width="100%"><p class="caption">
Figure 9.22: Same as earlier figure but with order of conditions on x-axis altered.
</p>
</div>
<p>And if we compare this new figure to the original, side-by-side, we see the difference:</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox4-scale3"></span>
<img src="appendix-0_files/figure-html/viobox4-scale3-1.png" alt="Switching factor orders" width="100%"><p class="caption">
Figure 9.23: Switching factor orders
</p>
</div>
</div>
<div id="controlling-the-legend" class="section level2" number="9.4">
<h2>
<span class="header-section-number">9.4</span> Controlling the Legend<a class="anchor" aria-label="anchor" href="#controlling-the-legend"><i class="fas fa-link"></i></a>
</h2>
<p><strong>Using the <code>guides()</code></strong></p>
<p>Whilst we are on the subject of changing order and position of elements of the figure, you might think about changing the position of a figure legend. There is actually a few ways of doing it but a simple approach is to use the the <code>guides()</code> function and add that to the ggplot chain. For example, if we run the below code and look at the output:</p>
<div class="sourceCode" id="cb105"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, alpha <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">guides</span><span class="op">(</span>fill <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-legend1"></span>
<img src="appendix-0_files/figure-html/viobox6-legend1-1.png" alt="Figure Legend removed using `guides()`" width="100%"><p class="caption">
Figure 9.24: Figure Legend removed using <code>guides()</code>
</p>
</div>
<p>We see the same display as Figure <a href="additional-advanced-plots-and-customisation-options.html#fig:viobox6-add">9.19</a> but with no legend. That is quite useful because the legend just repeats the x-axis and becomes redundant. The <code>guides()</code> function works but setting the legened associated with the <code>fill</code> layer (i.e. <code>fill = condition</code>) to <code>"none"</code>, basically removing it. One thing to note with this approach is that you need to set a guide for every legend, otherwise a legend will appear. What that means is that if you had set both <code>fill = condition</code> and <code>color = condition</code>, then you would need to set both <code>fill</code> and <code>color</code> to <code>"none"</code> within <code>guides()</code> as follows:</p>
<div class="sourceCode" id="cb106"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span>, color <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, alpha <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">guides</span><span class="op">(</span>fill <span class="op">=</span> <span class="st">"none"</span>, color <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-legend2"></span>
<img src="appendix-0_files/figure-html/viobox6-legend2-1.png" alt="Removing more than one legend with `guides()`" width="100%"><p class="caption">
Figure 9.25: Removing more than one legend with <code>guides()</code>
</p>
</div>
<p>Whereas if you hadn't used <code>guides()</code> you would see the following:</p>
<div class="sourceCode" id="cb107"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span>, color <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, alpha <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-legend3"></span>
<img src="appendix-0_files/figure-html/viobox6-legend3-1.png" alt="Figure with more than one Legend" width="100%"><p class="caption">
Figure 9.26: Figure with more than one Legend
</p>
</div>
<p>The key thing to note here is that in the above figure there is actually two legends (one for <code>fill</code> and one for <code>color</code>) but they are overlaid on top of each other as they are associated with the same variable. You can test this by removing either one of the options from the <code>guides()</code> function. One of the legends will still remain. So you need to turn them both off or you can use it to leave certain parts clear.</p>
<p><strong>Using the <code>theme()</code></strong></p>
<p>An alternative to the guides function is using the <code>theme()</code> function. The <code>theme()</code> function can actually be used to control a whole host of options in the plot, which we will come on to, but you can use it as a quick way to turn off the legend as follows:</p>
<div class="sourceCode" id="cb108"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">condition</span>, y<span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span>, color <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_violin</span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">.4</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_boxplot</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">.2</span>, fatten <span class="op">=</span> <span class="cn">NULL</span>, alpha <span class="op">=</span> <span class="fl">.5</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun <span class="op">=</span> <span class="st">"mean"</span>, geom <span class="op">=</span> <span class="st">"point"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_summary</span><span class="op">(</span>fun.data <span class="op">=</span> <span class="st">"mean_se"</span>, geom <span class="op">=</span> <span class="st">"errorbar"</span>, width <span class="op">=</span> <span class="fl">.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_viridis_d</span><span class="op">(</span>option <span class="op">=</span> <span class="st">"E"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme</span><span class="op">(</span>legend.position <span class="op">=</span> <span class="st">"none"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-legend4"></span>
<img src="appendix-0_files/figure-html/viobox6-legend4-1.png" alt="Removing the legend with `theme()`" width="100%"><p class="caption">
Figure 9.27: Removing the legend with <code>theme()</code>
</p>
</div>
<p>What you can see is that within the <code>theme()</code> function we set an argument for <code>legend.position</code> and we set that to <code>"none"</code> - again removing the legend entirely. One difference to note here is that it removes all aspects of the legend where as, as we said, using <code>guides()</code> allows you to control different parts of the legend (either leaving the <code>fill</code> or <code>color</code> showing or both). So using the <code>legend.position = "none"</code> is a bit more brute force and can be handy when you are using various different means of distinguishing between conditions of a variable and don't want to have to remove each aspect using the <code>guides()</code>.</p>
<p>An extension here of course is not just removing the legend, but moving the legend to a different position. This can be done by setting <code>legend.position = ...</code> to either <code>"top"</code>, <code>"bottom"</code>, <code>"left"</code> or <code>"right"</code> as shown:</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-legend5"></span>
<img src="appendix-0_files/figure-html/viobox6-legend5-1.png" alt="Legend position options using theme()" width="100%"><p class="caption">
Figure 9.28: Legend position options using theme()
</p>
</div>
<p>Or even as a coordinate within your figure expressed as a propotion of your figure - i.e. c(x = 0, y = 0) would be the bottom left of your figure and c(x = 1, y = 1) would be the top right, as shown here:</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:viobox6-legend6"></span>
<img src="appendix-0_files/figure-html/viobox6-legend6-1.png" alt="Legend position options using theme()" width="100%"><p class="caption">
Figure 9.29: Legend position options using theme()
</p>
</div>
<p>And so with a little trial and error you can position your legend where you want it without crashing into your figure, hopefully!</p>
</div>
<div id="setting-a-lab-theme-using-theme" class="section level2" number="9.5">
<h2>
<span class="header-section-number">9.5</span> Setting A Lab Theme using <code>theme()</code><a class="anchor" aria-label="anchor" href="#setting-a-lab-theme-using-theme"><i class="fas fa-link"></i></a>
</h2>
<p>The <code>theme()</code> function, as we mentioned, does a lot more than just change the position of a legend and can be used to really control a variety of elements and to eventually create your own "theme" for your figures - say you want to have a consistent look to your figures across your publications or across your lab posters. We will try to show some of that here, but first lets start with a very basic plot that we have seen before:</p>
<p><strong>THIS BIT NEEDS WORK</strong></p>
<div class="sourceCode" id="cb109"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># set up custom theme to add to all plots</span>
<span class="va">mytheme</span> <span class="op">&lt;-</span> <span class="fu">theme_minimal</span><span class="op">(</span>     <span class="co"># always start with a base theme_****</span>
  base_size <span class="op">=</span> <span class="fl">16</span>,             <span class="co"># 16-point font (adjusted for axes)</span>
  base_family <span class="op">=</span> <span class="st">"Helvetica"</span>   <span class="co"># font style</span>
<span class="op">)</span> <span class="op">+</span>                           <span class="co"># add more specific customisations with theme()</span>
<span class="fu">theme</span><span class="op">(</span>
  text             <span class="op">=</span> <span class="fu">element_text</span><span class="op">(</span>color <span class="op">=</span> <span class="st">"white"</span><span class="op">)</span>,  <span class="co"># most text</span>
  axis.text        <span class="op">=</span> <span class="fu">element_text</span><span class="op">(</span>color <span class="op">=</span> <span class="st">"grey60"</span><span class="op">)</span>, <span class="co"># axis label text</span>
  axis.line        <span class="op">=</span> <span class="fu">element_line</span><span class="op">(</span>color <span class="op">=</span> <span class="st">"grey60"</span><span class="op">)</span>, <span class="co"># x and y axes</span>
  plot.background  <span class="op">=</span> <span class="fu">element_rect</span><span class="op">(</span>fill <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span>,   <span class="co"># main background</span>
  panel.background <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,                <span class="co"># defaults to plot background fill</span>
  panel.grid       <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>                 <span class="co"># get rid of gridlines</span>
<span class="op">)</span>

<span class="co"># plot with custom theme</span>
<span class="fu">ggplot</span><span class="op">(</span><span class="va">diamonds</span>, <span class="fu">aes</span><span class="op">(</span><span class="va">carat</span>, <span class="va">price</span>, color <span class="op">=</span> <span class="va">cut</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_smooth</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> 
  <span class="va">mytheme</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:unnamed-chunk-1"></span>
<img src="appendix-0_files/figure-html/unnamed-chunk-1-1.png" alt="buidling something like this idea" width="100%"><p class="caption">
Figure 9.30: buidling something like this idea
</p>
</div>
</div>
<div id="easter-egg---overlaying-plots" class="section level2" number="9.6">
<h2>
<span class="header-section-number">9.6</span> Easter Egg - Overlaying Plots<a class="anchor" aria-label="anchor" href="#easter-egg---overlaying-plots"><i class="fas fa-link"></i></a>
</h2>
<p>Hopefully from some of the materials we have shown you, you will have found ways of presenting data in an informative manner - for example, we have shown violin plots and how they can be effective, when combined with boxplots, at displaying distributions. However, if you are familiar with other software you may be used to seeing this sort of information displayed differently, as perhaps a histogram with a normal curve overlaid. Whist the violin plots are better to convey that information we thought it might help to see alternative approaches here. Really it is about overlaying some of the plots we have already shown, but with some slight adjustments. FOr example, lets look at the histogram and density plot of reaction times we saw earlier - shown here side by side for convenience.</p>
<div class="sourceCode" id="cb110"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">a</span> <span class="op">&lt;-</span> <span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">10</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">labs</span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"+ geom_histogram()"</span><span class="op">)</span>

<span class="va">b</span> <span class="op">&lt;-</span> <span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_density</span><span class="op">(</span><span class="op">)</span><span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">labs</span><span class="op">(</span>subtitle <span class="op">=</span> <span class="st">"+ geom_density()"</span><span class="op">)</span>

<span class="va">a</span><span class="op">+</span><span class="va">b</span></code></pre></div>
<div class="inline-figure"><img src="appendix-0_files/figure-html/unnamed-chunk-2-1.png" width="100%" style="display: block; margin: auto;"></div>
<p>Now that in itself is fairly informative but perhaps takes up a lot of room so one option using some of the features of the patchwork library would be to inset the density plot in the top right of the histogram. We already showed a little of patchwork earlier so we won't repeat it here but all we are doing is placing one of the figures (the density plot) within the <code><a href="https://patchwork.data-imaginist.com/reference/inset_element.html">inset_element()</a></code> function and applying some appropriate values to position the inset - through a little trial and error - based on the bottom left corner of the plot area being <code>left = 0</code>, <code>bottom = 0</code>, and the top right corner being <code>right = 1</code>, <code>top = 1</code>:</p>
<div class="sourceCode" id="cb111"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">a</span> <span class="op">&lt;-</span> <span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">10</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span>

<span class="va">b</span> <span class="op">&lt;-</span> <span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_density</span><span class="op">(</span><span class="op">)</span><span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span>

<span class="va">a</span> <span class="op">+</span> <span class="fu"><a href="https://patchwork.data-imaginist.com/reference/inset_element.html">inset_element</a></span><span class="op">(</span><span class="va">b</span>, left <span class="op">=</span> <span class="fl">0.6</span>, bottom <span class="op">=</span> <span class="fl">0.6</span>, right <span class="op">=</span> <span class="fl">1</span>, top <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:overlay1"></span>
<img src="appendix-0_files/figure-html/overlay1-1.png" alt="Insetting a plot within a plot using `inset_element()` from the patchwork library" width="100%"><p class="caption">
Figure 9.31: Insetting a plot within a plot using <code><a href="https://patchwork.data-imaginist.com/reference/inset_element.html">inset_element()</a></code> from the patchwork library
</p>
</div>
<p>But of course that only works if there is space for the inset and it doesn't start overlapping on the main figure. This next approach fully overlays the density plot on top of the histogram. There is one main change though and that is the addition of <code>aes(y=..density..)</code> within <code>geom_histogram()</code>. This tells the histogram to now be plotted in terms of density and not count, meaning that the density plot and the histogram and now based on the same y-axis:</p>
<div class="sourceCode" id="cb112"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>y <span class="op">=</span> <span class="va">..density..</span><span class="op">)</span>,
                 binwidth <span class="op">=</span> <span class="fl">10</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_density</span><span class="op">(</span><span class="op">)</span><span class="op">+</span>
  <span class="fu">scale_x_continuous</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:overlay2"></span>
<img src="appendix-0_files/figure-html/overlay2-1.png" alt="A histogram with density plot overlaid" width="100%"><p class="caption">
Figure 9.32: A histogram with density plot overlaid
</p>
</div>
<p>The main thing to not in the above figure is that both the histogram and the density plot are based on the data you have collected. An alternative that you might want to look at it is plotting a normal distribution on top of the histogram based on the mean and standard deviation of the data. This is a bit more complicated but works as follows:</p>
<div class="sourceCode" id="cb113"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span><span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>y <span class="op">=</span> <span class="va">..density..</span><span class="op">)</span>,
                 binwidth <span class="op">=</span> <span class="fl">10</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_function</span><span class="op">(</span>
    fun <span class="op">=</span> <span class="va">dnorm</span>, 
    args <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>mean <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">dat_long</span><span class="op">$</span><span class="va">rt</span><span class="op">)</span>, 
                sd <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op">(</span><span class="va">dat_long</span><span class="op">$</span><span class="va">rt</span><span class="op">)</span><span class="op">)</span>
  <span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:overlay3"></span>
<img src="appendix-0_files/figure-html/overlay3-1.png" alt="A histogram with normal distribution based on the data overlaid" width="100%"><p class="caption">
Figure 9.33: A histogram with normal distribution based on the data overlaid
</p>
</div>
<p>The first part of this approach is identical to what we say above but instead of using <code>geom_density()</code> we are using a statistics function called <code>stat_function()</code> similar to ones we saw earlier when plotting means and standard deviations. What <code>stat_function()</code> is doing is taking the Normal distribution density function, <code>fun = dnorm</code> (read as function equals density normal), and then the mean of the data (<code>mean = mean(dat_long$rt)</code>) and the standard deviation of the data <code>sd = sd(dat_long$rt)</code> and creates a distribution based on those values. The <code>args</code> refers to the arguments that the <code>dnorm</code> function takes, and they are passed to the function as a list (<code><a href="https://rdrr.io/r/base/list.html">list()</a></code>). But from there, you can then start to alter the <code>linetype</code>, <code>color</code>, and thickness (<code>lwd = 3</code> for example) as you please.</p>
<div class="sourceCode" id="cb114"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu">aes</span><span class="op">(</span><span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_histogram</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>y <span class="op">=</span> <span class="va">..density..</span><span class="op">)</span>,
                 binwidth <span class="op">=</span> <span class="fl">10</span>, fill <span class="op">=</span> <span class="st">"white"</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">stat_function</span><span class="op">(</span>
    fun <span class="op">=</span> <span class="va">dnorm</span>, 
    args <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>mean <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">dat_long</span><span class="op">$</span><span class="va">rt</span><span class="op">)</span>, 
                sd <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op">(</span><span class="va">dat_long</span><span class="op">$</span><span class="va">rt</span><span class="op">)</span><span class="op">)</span>,
    color <span class="op">=</span> <span class="st">"red"</span>,
    lwd <span class="op">=</span> <span class="fl">3</span>,
    linetype <span class="op">=</span> <span class="fl">2</span>
  <span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:overlay4"></span>
<img src="appendix-0_files/figure-html/overlay4-1.png" alt="Changing the line of the `stat_function()`" width="100%"><p class="caption">
Figure 9.34: Changing the line of the <code>stat_function()</code>
</p>
</div>
</div>
<div id="easter-egg---a-dumbbell-plot" class="section level2" number="9.7">
<h2>
<span class="header-section-number">9.7</span> Easter Egg - A Dumbbell Plot<a class="anchor" aria-label="anchor" href="#easter-egg---a-dumbbell-plot"><i class="fas fa-link"></i></a>
</h2>
<p>A nice way of representing a change across different conditions, within participants or across timepoints, is the dumbbell chart. These figures can do a lot of heavy lifting in conveying patterns within the data and are not as hard to create in ggplot as they might first appear. The premise is that you need the start point, in terms of x (<code>x =</code>) and y (<code>y =</code>), and the end point, again in terms of x (<code>xend =</code>) and y (<code>yend =</code>). You draw a line between those two points using <code>geom_segment()</code> and then add a data point at the both ends of the line using <code>geom_point()</code>. So for example, we will use the average accuracy scores for the word and non-word conditions, for monolingual and bilinguals, to demonstrate. We could do the same figure for all participants but as we have 100 participants it can be a bit <strong>wild</strong>. We first need to create the averages using a little bit of data wrangling we have seen:</p>
<div class="sourceCode" id="cb115"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">dat_avg</span> <span class="op">&lt;-</span> <span class="va">dat</span> <span class="op">%&gt;%</span>
  <span class="fu">group_by</span><span class="op">(</span><span class="va">language</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu">summarise</span><span class="op">(</span>mean_acc_nonword <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">acc_nonword</span><span class="op">)</span>,
            mean_acc_word <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">acc_word</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<p>So our data looks as follows:</p>
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th align="center">language</th>
<th align="center">mean_acc_nonword</th>
<th align="center">mean_acc_word</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="center">monolingual</td>
<td align="center">84.87273</td>
<td align="center">94.87273</td>
</tr>
<tr class="even">
<td align="center">bilingual</td>
<td align="center">84.93333</td>
<td align="center">95.17778</td>
</tr>
</tbody>
</table></div>
With our average accuracies for non-word trials in **
<div class="kable-table">
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th align="left">nom</th>
</tr></thead>
<tbody><tr class="odd">
<td align="left">mean_acc_nonword</td>
</tr></tbody>
</table></div>
</div>
** and our average accuracies for word trials in **
<div class="kable-table">
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th align="left">nom</th>
</tr></thead>
<tbody><tr class="odd">
<td align="left">mean_acc_word</td>
</tr></tbody>
</table></div>
</div>
<p>**. And now we can create our dumbbell plot as follows:</p>
<div class="sourceCode" id="cb116"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">ggplot</span><span class="op">(</span><span class="va">dat_avg</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_segment</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">mean_acc_nonword</span>, y <span class="op">=</span> <span class="va">language</span>,
                   xend <span class="op">=</span> <span class="va">mean_acc_word</span>, yend <span class="op">=</span> <span class="va">language</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">mean_acc_nonword</span>, y <span class="op">=</span> <span class="va">language</span><span class="op">)</span>, color <span class="op">=</span> <span class="st">"red"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_point</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="va">mean_acc_word</span>, y <span class="op">=</span> <span class="va">language</span><span class="op">)</span>, color <span class="op">=</span> <span class="st">"blue"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">labs</span><span class="op">(</span>x <span class="op">=</span> <span class="st">"Change in Accuracy"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:dumbbell1"></span>
<img src="appendix-0_files/figure-html/dumbbell1-1.png" alt="A dumbbell plot of change in Average Accuracy from Non-word trials (red dots) to Word trials (blue dots) for monolingual and bilingual participants." width="100%"><p class="caption">
Figure 9.35: A dumbbell plot of change in Average Accuracy from Non-word trials (red dots) to Word trials (blue dots) for monolingual and bilingual participants.
</p>
</div>
<p>Which actually gives the least exciting figure ever as both groups showed the same change from the non-word trials (red dots) to the word trials (blue dots) but we can break the code down a bit just to highlight what we are doing, remembering the idea about layers. Layers one and two add the basic background and black line from the start point (x,y), the mean accuracy of non-word trials for the two conditions, to the end point (xend, yend), the mean accuracy of word trials for the two conditions:</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:dumbbell2"></span>
<img src="appendix-0_files/figure-html/dumbbell2-1.png" alt="Building the bars of our dumbbells. The (x,y) and (xend, yend) have been added to show the values you need to consider and enter to create the dumbbell" width="100%"><p class="caption">
Figure 9.36: Building the bars of our dumbbells. The (x,y) and (xend, yend) have been added to show the values you need to consider and enter to create the dumbbell
</p>
</div>
<p>and the remaining lines add the dots at the end of the dumbells and changes the x axis label to something useful:</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:dumbbell3"></span>
<img src="appendix-0_files/figure-html/dumbbell3-1.png" alt="Adding the weights to the dumbbells. Red dots are added in one layer to show Average Accuracy of Non-word trials, and blue dots are added in final layer to show Average Accuracy of Word trials." width="100%"><p class="caption">
Figure 9.37: Adding the weights to the dumbbells. Red dots are added in one layer to show Average Accuracy of Non-word trials, and blue dots are added in final layer to show Average Accuracy of Word trials.
</p>
</div>
<p>Of course, worth remembering, it is better to always think of the dumbbell as a start and end point, not left and right, as had accuracy gone down when moving from Non-word trials to Word trials then our bars would run the opposite direction. If you repeat the above process using reaction times instead of accuracy you will see what we mean.</p>
</div>
<div id="easter-egg---a-pie-chart" class="section level2" number="9.8">
<h2>
<span class="header-section-number">9.8</span> Easter Egg - A Pie Chart<a class="anchor" aria-label="anchor" href="#easter-egg---a-pie-chart"><i class="fas fa-link"></i></a>
</h2>
<p>Pie Charts are not the best form of visualisation as they generally require people to compare areas and/or angles which is a fairly unintuitive means of doing a comparison. The are so disliked in many fields that ggplot does not actually have a <code>geom_...()</code> function to create one. But, there is always somebody that wants to create a pie chart regardless and who are we to judge. So here would be the code to produce a pie chart of the demographic data we saw in the start of the paper:</p>
<div class="sourceCode" id="cb117"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">count_dat</span> <span class="op">&lt;-</span> <span class="va">dat</span> <span class="op">%&gt;%</span>
  <span class="fu">group_by</span><span class="op">(</span><span class="va">language</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu">count</span><span class="op">(</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu">ungroup</span><span class="op">(</span><span class="op">)</span> <span class="op">%&gt;%</span>
  <span class="fu">mutate</span><span class="op">(</span>percent <span class="op">=</span> <span class="op">(</span><span class="va">n</span><span class="op">/</span><span class="fu"><a href="https://rdrr.io/r/base/sum.html">sum</a></span><span class="op">(</span><span class="va">n</span><span class="op">)</span><span class="op">*</span><span class="fl">100</span><span class="op">)</span><span class="op">)</span>

<span class="fu">ggplot</span><span class="op">(</span><span class="va">count_dat</span>, <span class="fu">aes</span><span class="op">(</span>x <span class="op">=</span> <span class="st">""</span>, 
                      y <span class="op">=</span> <span class="va">percent</span>, 
                      fill <span class="op">=</span> <span class="va">language</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_bar</span><span class="op">(</span>width <span class="op">=</span> <span class="fl">1</span>, stat<span class="op">=</span><span class="st">"identity"</span><span class="op">)</span> <span class="op">+</span> 
  <span class="fu">coord_polar</span><span class="op">(</span><span class="st">"y"</span>, start <span class="op">=</span> <span class="fl">0</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme</span><span class="op">(</span>
    axis.title <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,
    panel.grid <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,
    panel.border <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,
    axis.ticks <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,
    axis.text.x <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>
  <span class="op">)</span> <span class="op">+</span>
  <span class="fu">geom_text</span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">75</span>, <span class="fl">25</span><span class="op">)</span>, 
                label <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="va">percent</span>, <span class="st">"%"</span><span class="op">)</span><span class="op">)</span>,
            size <span class="op">=</span> <span class="fl">6</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:piechart-full"></span>
<img src="appendix-0_files/figure-html/piechart-full-1.png" alt="A pie chart of the demographics" width="100%"><p class="caption">
Figure 9.38: A pie chart of the demographics
</p>
</div>
<p>Note that this is effectively creating a stacked bar chart with no x variable (i.e. <code>x = ""</code>) and then wrapping the y-axis into a circle (i.e. <code>coord_polar("y", start = 0)</code>). That is what the first three lines of the <code>ggplot()</code> code does:</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:pie-basic1"></span>
<img src="appendix-0_files/figure-html/pie-basic1-1.png" alt="The basis of a pie chart" width="100%"><p class="caption">
Figure 9.39: The basis of a pie chart
</p>
</div>
<p>The remainder of the code is used to remove the various panel and tick lines, and text, setting them all to <code>element_blank()</code> through the <code>theme()</code> functions we saw above, and to add new labelling text on top of the pie chart at specific y-values (i.e. <code>y = c(75,25)</code>). But remember, <strong>friends don't let friends make pie charts!</strong></p>

<div id="refs" class="references csl-bib-body hanging-indent" line-spacing="2">
<div id="ref-raincloudplots" class="csl-entry">
Allen, M., Poggiali, D., Whitaker, K., Marshall, T. R., van Langen, J., &amp; Kievit, R. A. (2021). Raincloud plots: A multi-platform tool for robust data visualization [version 2; peer review: 2 approved]. <em>Wellcome Open Research</em>, <em>4</em>. <a href="https://doi.org/10.12688/wellcomeopenres.15191.2">https://doi.org/10.12688/wellcomeopenres.15191.2</a>
</div>
<div id="ref-allison2012toward" class="csl-entry">
Allison, C., Auyeung, B., &amp; Baron-Cohen, S. (2012). Toward brief <span>“red flags”</span> for autism screening: The short autism spectrum quotient and the short quantitative checklist in 1,000 cases and 3,000 controls. <em>Journal of the American Academy of Child &amp; Adolescent Psychiatry</em>, <em>51</em>(2), 202–212.
</div>
<div id="ref-barrett2019six" class="csl-entry">
Barrett, T. S. (2019). <em>Six reasons to consider using r in psychological research</em>.
</div>
<div id="ref-NYT-R" class="csl-entry">
Bertini, E., &amp; Stefaner, M. (2015). Amanda cox on working with r, NYT projects, favorite data [podcast]. In <em>Data Stories</em>. <a href="https://datastori.es/ds-56-amanda-cox-nyt/">https://datastori.es/ds-56-amanda-cox-nyt/</a>
</div>
<div id="ref-munafo2017manifesto" class="csl-entry">
Munafò, M. R., Nosek, B. A., Bishop, D. V., Button, K. S., Chambers, C. D., Du Sert, N. P., Simonsohn, U., Wagenmakers, E.-J., Ware, J. J., &amp; Ioannidis, J. P. (2017). A manifesto for reproducible science. <em>Nature Human Behaviour</em>, <em>1</em>(1), 1–9.
</div>
<div id="ref-Nation2017" class="csl-entry">
Nation, Z. (2017). Perceptions. In <em>GitHub repository</em>. <a href="https://github.com/zonination/perceptions%20%20">https://github.com/zonination/perceptions  </a>; GitHub.
</div>
<div id="ref-newman2012bar" class="csl-entry">
Newman, G. E., &amp; Scholl, B. J. (2012). Bar graphs depicting averages are perceptually misinterpreted: The within-the-bar bias. <em>Psychonomic Bulletin &amp; Review</em>, <em>19</em>(4), 601–607.
</div>
<div id="ref-R-base" class="csl-entry">
R Core Team. (2021). <em>R: A language and environment for statistical computing</em>. R Foundation for Statistical Computing. <a href="https://www.R-project.org/">https://www.R-project.org/</a>
</div>
<div id="ref-robins2003learning" class="csl-entry">
Robins, A., Rountree, J., &amp; Rountree, N. (2003). Learning and teaching programming: A review and discussion. <em>Computer Science Education</em>, <em>13</em>(2), 137–172.
</div>
<div id="ref-RStudio" class="csl-entry">
RStudio Team. (2021). <em>RStudio: Integrated development environment for r</em>. RStudio, PBC. <a href="http://www.rstudio.com/">http://www.rstudio.com/</a>
</div>
<div id="ref-BBC-R" class="csl-entry">
Visual, B., &amp; Journalism, D. (2019). How the BBC visual and data journalism team works with graphics in r. In <em>Medium</em>. <a href="https://medium.com/bbc-visual-and-data-journalism/how-the-bbc-visual-and-data-journalism-team-works-with-graphics-in-r-ed0b35693535">https://medium.com/bbc-visual-and-data-journalism/how-the-bbc-visual-and-data-journalism-team-works-with-graphics-in-r-ed0b35693535</a>
</div>
<div id="ref-wickham2010layered" class="csl-entry">
Wickham, H. (2010). A layered grammar of graphics. <em>Journal of Computational and Graphical Statistics</em>, <em>19</em>(1), 3–28.
</div>
<div id="ref-ggplot2" class="csl-entry">
Wickham, H. (2016). <em>ggplot2: Elegant graphics for data analysis</em>. Springer-Verlag New York. <a href="https://ggplot2.tidyverse.org">https://ggplot2.tidyverse.org</a>
</div>
<div id="ref-tidyverse" class="csl-entry">
Wickham, H. (2017). <em>Tidyverse: Easily install and load the ’tidyverse’</em>. <a href="https://CRAN.R-project.org/package=tidyverse">https://CRAN.R-project.org/package=tidyverse</a>
</div>
<div id="ref-wickham2014tidy" class="csl-entry">
Wickham, H., &amp; others. (2014). Tidy data. <em>Journal of Statistical Software</em>, <em>59</em>(10), 1–23.
</div>
<div id="ref-wilkinson2005graph" class="csl-entry">
Wilkinson, L., Anand, A., &amp; Grossman, R. (2005). Graph-theoretic scagnostics. <em>IEEE Symposium on Information Visualization (InfoVis 05)</em>, 157–158.
</div>
<div id="ref-rminr" class="csl-entry">
Wills, A. (n.d.). Teaching research methods in r. In <em>rminr</em>. <a href="https://www.andywills.info/rminr/rminrinpsy.html">https://www.andywills.info/rminr/rminrinpsy.html</a>
</div>
</div>
</div>
</div>










<script>

/* update total correct if #webex-total_correct exists */
update_total_correct = function() {
  console.log("webex: update total_correct");

  if (t = document.getElementById("webex-total_correct")) {
    var correct = document.getElementsByClassName("webex-correct").length;
    var solvemes = document.getElementsByClassName("webex-solveme").length;
    var radiogroups = document.getElementsByClassName("webex-radiogroup").length;
    var selects = document.getElementsByClassName("webex-select").length;
    
    t.innerHTML = correct + " of " + (solvemes + radiogroups + selects) + " correct";
  }
}

/* webex-solution button toggling function */
b_func = function() {
  console.log("webex: toggle hide");
  
  var cl = this.parentElement.classList;
  if (cl.contains('open')) {
    cl.remove("open");
  } else {
    cl.add("open");
  }
}

/* function for checking solveme answers */
solveme_func = function(e) {
  console.log("webex: check solveme");

  var real_answers = JSON.parse(this.dataset.answer);
  var my_answer = this.value;
  var cl = this.classList;
  if (cl.contains("ignorecase")) {
    my_answer = my_answer.toLowerCase();
  }
  if (cl.contains("nospaces")) {
    my_answer = my_answer.replace(/ /g, "")
  }

  if (my_answer == "") {
    cl.remove("webex-correct");
    cl.remove("webex-incorrect");
  } else if (real_answers.includes(my_answer)) {
    cl.add("webex-correct");
    cl.remove("webex-incorrect");
  } else {
    cl.add("webex-incorrect");
    cl.remove("webex-correct");
  }

  // match numeric answers within a specified tolerance
  if(this.dataset.tol > 0){
    var tol = JSON.parse(this.dataset.tol);
    var matches = real_answers.map(x => Math.abs(x - my_answer) < tol)
    if (matches.reduce((a, b) => a + b, 0) > 0) {
      cl.add("webex-correct");
    } else {
      cl.remove("webex-correct");
    }
  }

  // added regex bit
  if (cl.contains("regex")){
    answer_regex = RegExp(real_answers.join("|"))
    if (answer_regex.test(my_answer)) {
      cl.add("webex-correct");
    }
  }

  update_total_correct();
}

/* function for checking select answers */
select_func = function(e) {
  console.log("webex: check select");
  
  var cl = this.classList
  
  /* add style */
  cl.remove("webex-incorrect");
  cl.remove("webex-correct");
  if (this.value == "answer") {
    cl.add("webex-correct");
  } else if (this.value != "blank") {
    cl.add("webex-incorrect");
  }
  
  update_total_correct();
}

/* function for checking radiogroups answers */
radiogroups_func = function(e) {
  console.log("webex: check radiogroups");

  var checked_button = document.querySelector('input[name=' + this.id + ']:checked');
  var cl = checked_button.parentElement.classList;
  var labels = checked_button.parentElement.parentElement.children;
  
  /* get rid of styles */
  for (i = 0; i < labels.length; i++) {
    labels[i].classList.remove("webex-incorrect");
    labels[i].classList.remove("webex-correct");
  }
  
  /* add style */
  if (checked_button.value == "answer") {
    cl.add("webex-correct");
  } else {
    cl.add("webex-incorrect");
  }
  
  update_total_correct();
}

window.onload = function() {
  console.log("onload");
  /* set up solution buttons */
  var buttons = document.getElementsByTagName("button");

  for (var i = 0; i < buttons.length; i++) {
    if (buttons[i].parentElement.classList.contains('webex-solution')) {
      buttons[i].onclick = b_func;
    }
  }

  /* set up webex-solveme inputs */
  var solveme = document.getElementsByClassName("webex-solveme");

  for (var i = 0; i < solveme.length; i++) {
    /* make sure input boxes don't auto-anything */
    solveme[i].setAttribute("autocomplete","off");
    solveme[i].setAttribute("autocorrect", "off");
    solveme[i].setAttribute("autocapitalize", "off");
    solveme[i].setAttribute("spellcheck", "false");
    solveme[i].value = "";

    /* adjust answer for ignorecase or nospaces */
    var cl = solveme[i].classList;
    var real_answer = solveme[i].dataset.answer;
    if (cl.contains("ignorecase")) {
      real_answer = real_answer.toLowerCase();
    }
    if (cl.contains("nospaces")) {
      real_answer = real_answer.replace(/ /g, "");
    }
    solveme[i].dataset.answer = real_answer;

    /* attach checking function */
    solveme[i].onkeyup = solveme_func;
    solveme[i].onchange = solveme_func;
  }
  
  /* set up radiogroups */
  var radiogroups = document.getElementsByClassName("webex-radiogroup");
  for (var i = 0; i < radiogroups.length; i++) {
    radiogroups[i].onchange = radiogroups_func;
  }
  
  /* set up selects */
  var selects = document.getElementsByClassName("webex-select");
  for (var i = 0; i < selects.length; i++) {
    selects[i].onchange = select_func;
  }

  update_total_correct();
}

</script><script>
$( document ).ready(function() {
  var cite = ' ';
  var psyteachr = ' <a href="https://psyteachr.github.io/"><img src="images/logos/psyteachr_logo.png" style="height: 31px; color: white;" alt="psyTeachR: Reproducible Research" /></a>';
  var license = ' <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/" target="blank"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png"></a>';

  $("footer div.row div:eq(1) p").html(
    psyteachr + license + cite
  );

  function move_sidebar() {
    var w = window.innerWidth;
    if (w < 992) {
      $("#toc").appendTo($("#main-nav"));
    } else {
      $("#toc").appendTo($("div.sidebar-chapter"));
    }
  }
  move_sidebar();
  window.onresize = move_sidebar;
});
</script><div class="chapter-nav">
<div class="prev"><a href="additional-resources.html"><span class="header-section-number">8</span> Additional resources</a></div>
<div class="empty"></div>
</div></main><div class="col-md-3 col-lg-2 d-none d-md-block sidebar sidebar-chapter">
    <nav id="toc" data-toggle="toc" aria-label="On this page"><h2>On this page</h2>
      <ul class="nav navbar-nav">
<li><a class="nav-link" href="#additional-advanced-plots-and-customisation-options"><span class="header-section-number">9</span> Additional advanced plots and customisation options</a></li>
<li><a class="nav-link" href="#adding-lines-to-plots"><span class="header-section-number">9.1</span> Adding lines to plots</a></li>
<li><a class="nav-link" href="#zooming-in-and-out"><span class="header-section-number">9.2</span> Zooming in and out</a></li>
<li><a class="nav-link" href="#setting-the-axis-values"><span class="header-section-number">9.3</span> Setting the axis values</a></li>
<li><a class="nav-link" href="#controlling-the-legend"><span class="header-section-number">9.4</span> Controlling the Legend</a></li>
<li><a class="nav-link" href="#setting-a-lab-theme-using-theme"><span class="header-section-number">9.5</span> Setting A Lab Theme using theme()</a></li>
<li><a class="nav-link" href="#easter-egg---overlaying-plots"><span class="header-section-number">9.6</span> Easter Egg - Overlaying Plots</a></li>
<li><a class="nav-link" href="#easter-egg---a-dumbbell-plot"><span class="header-section-number">9.7</span> Easter Egg - A Dumbbell Plot</a></li>
<li><a class="nav-link" href="#easter-egg---a-pie-chart"><span class="header-section-number">9.8</span> Easter Egg - A Pie Chart</a></li>
</ul>

      <div class="book-extra">
        <ul class="list-unstyled">
<li><a id="book-source" href="https://github.com/psyteachr/introdataviz/blob/master/book/appendix-0.Rmd">View source <i class="fab fa-github"></i></a></li>
          <li><a id="book-edit" href="https://github.com/psyteachr/introdataviz/edit/master/book/appendix-0.Rmd">Edit this page <i class="fab fa-github"></i></a></li>
        </ul>
</div>
    </nav>
</div>

</div>
</div> <!-- .container -->

<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">

  <div class="col-12 col-md-6 mt-3">
    <p>"<strong>Data visualisation using R, for researchers who don’t use R</strong>" was written by Emily Nordmann, Phil McAleer, Wilhelmiina Toivo, Helena Paterson, Lisa DeBruine. It was last built on 2021-09-18.</p>
  </div>

  <div class="col-12 col-md-6 mt-3">
    <p>This book was built by the <a class="text-light" href="https://bookdown.org">bookdown</a> R package.</p>
  </div>

</div></div>
</footer>
</body>
</html>
